﻿/*
 * OpenDoPE Word Add-In
 * Copyright (C) 2010-2011  Plutext Pty Ltd
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * For details, see http://www.gnu.org/licenses/gpl-2.0.html
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NLog;

using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;


namespace OpenDoPE_Wed
{
    public partial class UserControlRepeatButtons : UserControl
    {

        static Logger log = LogManager.GetLogger("OpenDoPE_Wed");

        WedTaskPane taskPane;

        public UserControlRepeatButtons(WedTaskPane taskPane)
            //Word.ContentControl currentCC, XmlEditorControl xec)
        {
            this.taskPane = taskPane;

            //this.currentCC = currentCC;
            //this.xec = xec;

            InitializeComponent();
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            log.Debug("repeat clicked..");

            Office.CustomXMLPart cxp = taskPane.activeCxp;
            Word.ContentControl currentCC = taskPane.currentCC;
            XmlEditorControl xec = taskPane.xmlEditor;

            if (!xec.isWellFormed()) return;

            // Save the XML
            xec.commitChanges();

            // Now write the xpath 

            if (!xec.isInputOK() )
            {
                return;
            }

            if (currentCC == null)
            {
                log.Error("currentCC not set?!");
                return;
            }

            TagData td = new TagData(currentCC.Tag);
            String existingID = td.get("od:repeat");
            if (existingID != null && xec.textBoxXPathID.Text.Trim().Equals(existingID))
            {
                // The ID hasn't changed; we silently reuse it
            }
            else
            {
                // They've typed another ID
                // Does it exist already?  
                if (XPathPartUtils.getByID(taskPane.xpathsPart, xec.textBoxXPathID.Text.Trim()) != null)
                {
                    // If so, overwrite that one, or choose again?
                    // Press OK to overwrite, or cancel to choose another ID
                    if (MessageBox.Show("Press Cancel to type another.", "ID exists", MessageBoxButtons.OKCancel)
                            == DialogResult.Cancel)
                    {
                        return;
                    }
                }
            }
            
            // Write tag
            td.remove("od:repeat");
            td.remove("od:condition");
            td.set("od:repeat", xec.textBoxXPathID.Text.Trim() );
            currentCC.Tag = td.asQueryString();

            // Question
            string questionID = null;
            if (taskPane.questions)
            {
                FormQuestion formQuestion = new FormQuestion(taskPane.questionsPart,
                    xec.xpathTextBox.Text);
                formQuestion.ShowDialog();
                // TODO - handle cancel

                formQuestion.updateQuestionsPart(formQuestion.getQuestion());
                // TODO ensure questionID is an NCName
                questionID = formQuestion.textBoxQID.Text;
                formQuestion.Dispose();
            }

            // Also add to XPaths

            string prefixMappings = xec.getPrefixMappings();

            XPathPartUtils.updateXPathsPart(taskPane.xpathsPart,
                xec.xpathTextBox.Text,
                xec.textBoxXPathID.Text.Trim(),
                cxp.Id, prefixMappings, questionID);

            Mbox.ShowSimpleMsgBoxInfo("Bound!");
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {

            Word.ContentControl currentCC = taskPane.currentCC;
            XmlEditorControl xec = taskPane.xmlEditor;

            if (currentCC == null)
            {
                log.Error("currentCC not set?!");
                return;
            }

            // Write tag
            TagData td = new TagData(currentCC.Tag);

            try
            {
                if (td.get("od:repeat") == null)
                {
                    Mbox.ShowSimpleMsgBoxInfo("This content control isn't a repeat.");
                    return;
                }
            } catch (Exception ex) {
            }

            td.remove("od:repeat");
            currentCC.Tag = td.asQueryString();

            Mbox.ShowSimpleMsgBoxInfo("Repeat deleted from this content control.");


        }
    }
}
